sleepinterval = 10 * 60 --(10 minutos = 600 segundos)

local beds_list = {
	{ "Cama Vermelha", "red", "camavermelha"},
	{ "Cama Laranja", "orange", "camalaranja"},	
	{ "Cama Amarela", "yellow", "camaamarela"},
	{ "Cama Verde", "green", "camaverde"},
	{ "Cama Azul", "blue", "camaazul"},
	{ "Cama Purpura", "violet", "camapurpura"},
	{ "Cama Preta", "black", "camapreta"},
	{ "Cama Cinza", "grey", "camacinza"},
	{ "Cama Branca", "white", "camabranca"},
}

for i in ipairs(beds_list) do
	local beddesc = beds_list[i][1]
	local colour = beds_list[i][2]
	local summonname = beds_list[i][3]

	minetest.register_node("lunodecor:bed_bottom_"..colour, {
		description = beddesc.." (Cura HP a cada 10 minutos)",
		drawtype = "nodebox",
		tiles = {"beds_bed_top_bottom_"..colour..".png", "default_wood.png",  "beds_bed_side_"..colour..".png",  "beds_bed_side_"..colour..".png",  "beds_bed_side_"..colour..".png",  "beds_bed_side_"..colour..".png"},
		paramtype = "light",
		paramtype2 = "facedir",
		stack_max = 1,
		groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
		sounds = default.node_sound_wood_defaults(),
		node_box = {
			type = "fixed",
			fixed = {
				-- bed
				{-0.5, 0.0, -0.5, 0.5, 0.3125, 0.5},
				
				-- legs
				{-0.5, -0.5, -0.5, -0.4, 0.0, -0.4},
				{0.4, 0.0, -0.4, 0.5, -0.5, -0.5},
			}
		},
		selection_box = {
			type = "fixed",
			fixed = {
				{-0.5, -0.5, -0.5, 0.5, 0.3125, 1.5},
			}
		},

		after_place_node = function(pos, placer, itemstack)
			local node = minetest.env:get_node(pos)
			local p = {x=pos.x, y=pos.y, z=pos.z}
			local param2 = node.param2
			node.name = "lunodecor:bed_top_"..colour
			if param2 == 0 then
				pos.z = pos.z+1
			elseif param2 == 1 then
				pos.x = pos.x+1
			elseif param2 == 2 then
				pos.z = pos.z-1
			elseif param2 == 3 then
				pos.x = pos.x-1
			end
			if minetest.registered_nodes[minetest.env:get_node(pos).name].buildable_to  then
				minetest.env:set_node(pos, node)
				
				local meta = minetest.env:get_meta(p)
				local ownername = placer:get_player_name()
				--local bedtime = os.time()
				--print("bed.ownername = "..ownername)
				--print("bed.bedtime = "..bedtime)
				meta:set_string("infotext", "Cama de '"..ownername.."'")
				meta:set_string("owner", ownername)
				meta:set_string("bedtime", os.time())
				--meta:set_float("bedtime", os.time())
			else
				minetest.env:remove_node(p)
				return true
			end
		end,
			
		on_destruct = function(pos)
			local node = minetest.env:get_node(pos)
			local param2 = node.param2
			if param2 == 0 then
				pos.z = pos.z+1
			elseif param2 == 1 then
				pos.x = pos.x+1
			elseif param2 == 2 then
				pos.z = pos.z-1
			elseif param2 == 3 then
				pos.x = pos.x-1
			end
			if( minetest.env:get_node({x=pos.x, y=pos.y, z=pos.z}).name == "lunodecor:bed_top_"..colour ) then
				if( minetest.env:get_node({x=pos.x, y=pos.y, z=pos.z}).param2 == param2 ) then
					minetest.env:remove_node(pos)
				end	
			end
		end,
		on_rightclick = function(pos, node, clicker)
			if clicker~=nil and clicker:is_player() then
				local meta = minetest.env:get_meta(pos)
				if meta ~= nil then
					local ownername = meta:get_string("owner")
					local clickername = clicker:get_player_name()
					if ownername~=nil and ownername == clickername then
						if clicker:get_hp() < 20 then
							local bedtime = meta:get_string("bedtime")
							local now = os.time()
							local timetest = (bedtime + sleepinterval) - now
							if bedtime~=nil and tonumber(bedtime)>=0 then
								if now > bedtime + sleepinterval then
									meta:set_float("bedtime", now)
									clicker:set_hp(20)
								else
									minetest.chat_send_player(clickername, "Faltam "..timetest.." segundos para voce poder repousar nesta cama...")
								end
							end
						else
							minetest.chat_send_player(clickername, "Voce ja esta descansado o suficiente!")
						end
					else
						minetest.chat_send_player(clickername, "Esta cama nao pertence a voce!")
					end
				end
			end
		end
	})
	
	minetest.register_node("lunodecor:bed_top_"..colour, {
		drawtype = "nodebox",
		tiles = {"beds_bed_top_top_"..colour..".png", "default_wood.png",  "beds_bed_side_top_r_"..colour..".png",  "beds_bed_side_top_l_"..colour..".png",  "beds_bed_top_front.png",  "beds_bed_side_"..colour..".png"},
		paramtype = "light",
		paramtype2 = "facedir",
		groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
		sounds = default.node_sound_wood_defaults(),
		node_box = {
			type = "fixed",
			fixed = {
						-- bed
						{-0.5, 0.0, -0.5, 0.5, 0.3125, 0.5},
						{-0.4375, 0.3125, 0.1, 0.4375, 0.4375, 0.5},
						
						-- legs
						{-0.4, 0.0, 0.4, -0.5, -0.5, 0.5},
						{0.5, -0.5, 0.5, 0.4, 0.0, 0.4},
					}
		},
		selection_box = {
			type = "fixed",
			fixed = {
						{0, 0, 0, 0, 0, 0},
					}
		},
	})
	
	minetest.register_alias("lunodecor:bed_"..colour, "lunodecor:bed_bottom_"..colour)
	minetest.register_alias(summonname, "lunodecor:bed_"..colour)
	
	minetest.register_craft({
		output = "lunodecor:bed_"..colour,
		recipe = {
			{"wool:"..colour, "wool:"..colour, "wool:white", },
			{"default:stick", "", "default:stick", }
		}
	})
	
	minetest.register_craft({
		output = "lunodecor:bed_"..colour,
		recipe = {
			{"wool:white", "wool:"..colour, "wool:"..colour, },
			{"default:stick", "", "default:stick", }
		}
	})
	
end

minetest.register_alias("lunodecor:bed_bottom", "lunodecor:bed_bottom_white")
minetest.register_alias("lunodecor:bed_top", "lunodecor:bed_top_white")
minetest.register_alias("lunodecor:bed", "lunodecor:bed_bottom_white")
minetest.register_alias("bed", "lunodecor:bed")
minetest.register_alias("cama", "lunodecor:bed")
